#include "TcpServer.hpp"
#include "Http.hpp"


void Useage(const std::string &s)
{
    std::cout << "Useage" << ":\n\t" << s << " port\n";
}

std::shared_ptr<HttpResponse> Login(std::shared_ptr<HttpRequest> req)
{
    std::string message;
    if(req->Args().size())
        message = req->Args();
    else if(req->Content().size())
        message = req->Content();
    else
    {}

    //处理数据

    auto response = Factory::CreateHttpResponse();
    response->AddStatusLine(200, "OK");
    response->AddHeadler("Content-Type", "text/html");
    std::string content = "<html><body><h1>This is a response!!!!</h1></body></html>";
    response->AddHeadler("Content-Length", std::to_string(content.size()));
    response->AddContent(content);

    return response;
}

int main(int argc, char *argv[])
{
    std::cout << "pid : " << getpid() << std::endl;
    if (argc != 2)
    {
        Useage(argv[0]);
        exit(0);
    }
    uint16_t port = std::stoi(argv[1]);

    HttpServer http_server;
    http_server.AddOption("/login", Login);
    std::unique_ptr<TcpServer> usvr = std::make_unique<TcpServer>(port, std::bind(&HttpServer::HandlerRequest, &http_server, std::placeholders::_1));
    usvr->Loop();

    return 0;
}